// ------------------------不要这这个方法获得月，用下一个
// 当月1号至今天的时间
// 输出结果
// [
//   [
//     '2023-01',
//     '2023-06'
//   ],
//   [
//     '2023-06-01',
//     '2023-06-14'
//   ]
// ]
export function getTime() {
  const date = new Date()
  const y = date.getFullYear()
  const m = (date.getMonth() + 1) < 10 ? ('0' + (date.getMonth() + 1)) : (date.getMonth() + 1)
  const d = date.getDate() < 10 ? ('0' + date.getDate()) : date.getDate()
  const start_month = y + '-' + '01' // 开始月份：本年第一月份
  const end_month = `${y}-${m}` // 结束月份： 本年当前月份

  const start_day = y + '-' + m + '-' + '01' // 开始日期： 本月第一天
  const end_day = `${y}-${m}-${d}` // 结束日期： 本月今天

  const month = [start_month, end_month] // 默认月份区间
  const day = [start_day, end_day] // 默认日期区间
  return [month, day]
}
// ----------------------------每月1日到当前的时间
// 输出格式{ start: '2023-06-01 00:00:00', end: '2023-06-14 23:59:59' }
export function getMonth() {
  const today = new Date()
  const start = new Date(today.getFullYear(), today.getMonth(), 1)
  const end = new Date(today)

  const formatTime = function(date) {
    const year = date.getFullYear()
    const month = padZero(date.getMonth() + 1)
    const day = padZero(date.getDate())

    return `${year}-${month}-${day}`
  }

  function padZero(num) {
    return num < 10 ? '0' + num : num
  }

  const startTime = formatTime(start) + ' 00:00:00'
  const endTime = formatTime(end) + ' 23:59:59'

  return { start: startTime, end: endTime }
}
// 输出格式{ start: '2023-06-01+00:00:00', end: '2023-06-14+23:59:59' }
export function getMonth1() {
  const today = new Date()
  const start = new Date(today.getFullYear(), today.getMonth(), 1)
  const end = new Date(today)

  const formatTime = function(date) {
    const year = date.getFullYear()
    const month = padZero(date.getMonth() + 1)
    const day = padZero(date.getDate())

    return `${year}-${month}-${day}`
  }

  function padZero(num) {
    return num < 10 ? '0' + num : num
  }

  const startTime = formatTime(start) + '+00:00:00'
  const endTime = formatTime(end) + '+23:59:59'

  return { start: startTime, end: endTime }
}
// -------------不带具体时间每月1日
export function getMonthnum() {
  const today = new Date()
  const start = new Date(today.getFullYear(), today.getMonth(), 1)
  const end = new Date(today)

  const formatTime = function(date) {
    const year = date.getFullYear()
    const month = padZero(date.getMonth() + 1)
    const day = padZero(date.getDate())

    return `${year}-${month}-${day}`
  }

  function padZero(num) {
    return num < 10 ? '0' + num : num
  }

  const startTime = formatTime(start)
  const endTime = formatTime(end)

  return { start: startTime, end: endTime }
}

// ------------------------
// 当前时间和前两天的时间
// 返回格式
// {
//     "start": "2023-06-14",
//     "end": "2023-06-12"
// }
export function getTimeRange() {
  const today = new Date()
  const twoDaysAgo = new Date(today.getTime() - (2 * 24 * 60 * 60 * 1000))
  const start = `${twoDaysAgo.getFullYear()}-${twoDaysAgo.getMonth() + 1}-${twoDaysAgo.getDate()} 00:00:00`
  const end = `${today.getFullYear()}-${today.getMonth() + 1}-${today.getDate()} 23:59:59`
  return { start, end }
}

// -------------------------
// 放回当天的时间
// 格式为
// {start: '2023-06-14 00:00:00', end: '2023-06-14 23:59:59'}
export function getToday() {
  const today = new Date()
  const year = today.getFullYear()
  const month = (`0${today.getMonth() + 1}`).slice(-2)
  const day = (`0${today.getDate()}`).slice(-2)

  const startDate = `${year}-${month}-${day} 00:00:00`
  const endDate = `${year}-${month}-${day} 23:59:59`

  return { start: startDate, end: endDate }
}

// 从周一到当前的时间
export function getCurrentWeekTimeRange() {
  const now = new Date() // 获取当前日期对象
  const dayOfWeek = now.getDay() // 获取星期几，0代表周日，1代表周一，以此类推
  const diff = dayOfWeek === 0 ? 6 : dayOfWeek - 1 // 获取距离本周一相差的天数

  const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - diff) // 计算本周一日期对象
  const startStr = formatDate(start) + ' 00:00:00' // 格式化日期为字符串，拼接零点时间字符串

  const endStr = formatDate(now) + ' 23:59:59' // 格式化当前日期为字符串，拼接23:59:59字符串

  return {
    start: startStr,
    end: endStr
  }
}
// 格式化日期对象为 'yyyy-mm-dd' 形式的字符串
function formatDate(date) {
  const year = date.getFullYear()
  const month = (date.getMonth() + 1).toString().padStart(2, '0')
  const day = date.getDate().toString().padStart(2, '0')
  return `${year}-${month}-${day}`
}
// 从周一到当前的时间,不带具体时间
export function getCurrentTimeRange() {
  const now = new Date() // 获取当前日期对象
  const dayOfWeek = now.getDay() // 获取星期几，0代表周日，1代表周一，以此类推
  const diff = dayOfWeek === 0 ? 6 : dayOfWeek - 1 // 获取距离本周一相差的天数

  const start = new Date(now.getFullYear(), now.getMonth(), now.getDate() - diff) // 计算本周一日期对象
  const startStr = formatDate(start) // 格式化日期为字符串，拼接零点时间字符串

  const endStr = formatDate(now) // 格式化当前日期为字符串，拼接23:59:59字符串

  return {
    start: startStr,
    end: endStr
  }
}
// ---------------1月1日到当前时间
// 返回格式 { start: '2023-01-01 00:00:00', end: '2023-06-14 23:59:59' }
export function getYear() {
  const today = new Date()
  const start = new Date(today.getFullYear(), 0, 1)
  const end = new Date(today)

  const formatTime = function(date) {
    const year = date.getFullYear()
    const month = padZero(date.getMonth() + 1)
    const day = padZero(date.getDate())

    return `${year}-${month}-${day}`
  }

  function padZero(num) {
    return num < 10 ? '0' + num : num
  }

  const startTime = formatTime(start) + ' 00:00:00'
  const endTime = formatTime(end) + ' 23:59:59'

  return { start: startTime, end: endTime }
}

// ----------------一月一日不带具体日期
// 返回格式 { start: '2023-01-01 00:00:00', end: '2023-06-14 23:59:59' }
export function getYearnum() {
  const today = new Date()
  const start = new Date(today.getFullYear(), 0, 1)
  const end = new Date(today)

  const formatTime = function(date) {
    const year = date.getFullYear()
    const month = padZero(date.getMonth() + 1)
    const day = padZero(date.getDate())

    return `${year}-${month}-${day}`
  }

  function padZero(num) {
    return num < 10 ? '0' + num : num
  }

  const startTime = formatTime(start)
  const endTime = formatTime(end)

  return { start: startTime, end: endTime }
}

